package com.hoodemia.ui;

import net.code.btalk.ui.MessageTextField;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.TextField;

public class SuperDuperFieldManager extends Manager {
	private long style;
	
	/*private Field firstTextField;
	private Field secondTextField;
	
	private Field firstLabelField;
	private Field secondLabelField;
	
	private Field firstButtonField;
	private Field secondButtonField;*/
	
//	private static long TWO_LABEL_FIELD = 1000;
//	private static long TEXT_FIELD_AND_BUTTON = 1001;
//	private static long TEXT_FIELD_AND_TWO_BUTTON = 1002;
	
	public static long JUSTIFY_LEFT_AND_RIGHT = 2000;
	public static long JUSTIFY_CENTER = 2001;
	public static long JUSTIFY_LEFT = 2002;
	public static long JUSTIFY_RIGHT = 2003;
	
	private static int IMAGEBUTTON_WIDTH = 100;
	private static int TEXTBUTTON_WIDTH = 150;
	
	public SuperDuperFieldManager(long style){
		super(0);
		this.style = style;
		// TODO Auto-generated constructor stub
	}
	
	public SuperDuperFieldManager() {
		super(0);
	}

	protected void sublayout(int width, int height) {
		// TODO Auto-generated method stub
		Field firstField;
		Field secondField;
		
		if(this.getFieldCount() == 1) {
			firstField = this.getField(0);
			
			this.layoutChild(firstField, width, height);
			this.setPositionChild(firstField, 0, 0);
			
			setExtent(width, firstField.getPreferredHeight());
		} else if (this.getFieldCount() == 2 && this.style == JUSTIFY_LEFT_AND_RIGHT) {
			firstField = this.getField(0);
			secondField = this.getField(1);
			
			int firstFieldWidth = 0, firstFieldHeight = 0, firstFieldXPos = 0, firstFieldYPos = 0;
			int secondFieldWidth = 0, secondFieldHeight = 0, secondFieldXPos = 0, secondFieldYPos = 0;
			int managerWidth = 0, managerHeight = 0;
			
			/*if(firstField instanceof TextField) {
				firstTextField = firstField; 
			} else if (firstField instanceof LabelField) {
				firstLabelField = firstField;
			} else if (firstField instanceof ButtonField) {
				firstButtonField = firstField;
			}
			
			if(secondField instanceof TextField) {
				secondTextField = firstField; 
			} else if (secondField instanceof LabelField) {
				secondLabelField = firstField;
			} else if (secondField instanceof ButtonField) {
				secondButtonField = firstField;
			}*/
			
			if(firstField instanceof LabelField && secondField instanceof LabelField) {
				firstFieldWidth = width - secondField.getPreferredWidth();
				firstFieldHeight = firstField.getPreferredHeight();

				secondFieldWidth = secondField.getPreferredWidth();
				secondFieldHeight = secondField.getPreferredHeight();

				firstFieldXPos = 0;
				firstFieldYPos = 0;

				secondFieldXPos = width - secondField.getPreferredWidth();
				secondFieldYPos = 0;

				managerWidth = width;
				managerHeight = firstField.getPreferredHeight() > secondField
						.getPreferredHeight() ? firstField.getPreferredHeight()
						: secondField.getPreferredHeight();
			} else if ((firstField instanceof TextField || firstField instanceof MessageTextField) && secondField instanceof ButtonField) {
				firstFieldWidth = 4 * width / 5;
				firstFieldHeight = firstField.getPreferredHeight();

				secondFieldWidth = width / 5;
				secondFieldHeight = secondField.getPreferredHeight();

				firstFieldXPos = 0;
				firstFieldYPos = 0;

				secondFieldXPos = 4 * width / 5;
				secondFieldYPos = 0;

				managerWidth = width;
//				managerHeight = firstField.getPreferredHeight() > secondField
//				.getPreferredHeight() ? firstField.getPreferredHeight()
//				: secondField.getPreferredHeight();
				managerHeight = firstField.getPreferredHeight() > secondField
						.getPreferredHeight() ? firstField.getPreferredHeight() + 2
						: secondField.getPreferredHeight() + 2;
			}
			
			this.layoutChild(firstField, firstFieldWidth, firstFieldHeight);
			this.layoutChild(secondField, secondFieldWidth, secondFieldHeight);

			this.setPositionChild(firstField, firstFieldXPos, firstFieldYPos);
			this.setPositionChild(secondField, secondFieldXPos, secondFieldYPos);

			setExtent(managerWidth, managerHeight);
		}
		/*if (getFieldCount() > 0) {
			Field centeredField = getField(0); // get the first (and only) field
			layoutChild(centeredField, width, height); // set the field's width
														// and height
			setPositionChild(centeredField,
					(width - centeredField.getWidth()) / 2, 0); // center the
																// field
																// horizontally
			setExtent(width, centeredField.getHeight()); // set the size of this
															// manager to use
															// the entire screen
															// width
		}*/
	}

}
